#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stropts.h>
#include<linux/fb.h>
#include <sys/mman.h>
#include<math.h>

#define LCD "/dev/ubuntu_lcd"
#define BULE 0x000000ff
#define GREEN 0x0000ff00
#define RED 0x00ff0000
typedef unsigned int* lcd;

int size=800*480*4;
int lcdfd=-1;

lcd open_lcd(){
    lcdfd = open(LCD,O_RDWR);
    if(-1 == lcdfd){
        perror("open err");
        return NULL;
    }
    printf("设备打开成功\n");

    // struct fb_var_screeninfo info;
    // int ret = ioctl(lcdfd,FBIOGET_VSCREENINFO,&info);

    // if(-1 == ret){
    //     perror("获取设备信息失败");
    //     return NULL;
    // }

    // size=info.xres_virtual*info.yres_virtual*(info.bits_per_pixel/8);

    lcd p=mmap(NULL,size,PROT_READ | PROT_WRITE,MAP_SHARED,lcdfd,0);
    if(MAP_FAILED == p){
        perror("映射内存失败");
        return NULL;
    }

    printf("映射成功\n");
    return p;
}

int close_lcd(lcd p){

    int ret = munmap(p,size);
    if(-1 == ret){
        perror("解除映射失败");
    }
    printf("解除映射成功\n");
    ret = close(lcdfd);
    if(-1 == ret){
        perror("关闭设备失败\n");
        return -1;
    }
    printf("设备已关闭\n");
    return 0;

}

void circle(int x,int y,lcd p,int radius){
    for(int i =0;i < size/4;i++){
        p[i]=RED+BULE+GREEN;
        int nowx=(i+1)%800;
        int nowy=(i+1)/800;
        int len=abs( sqrt( (nowx - x)*(nowx - x)+(nowy - y)*(nowy -y) ) );
        if( len <= radius){
            p[i]=RED;
        }

    }


}



int main(int argc,char const argv[]){
    lcd p=open_lcd();
    int n=240;
    while(1){

        if(n > 480){
            n=1;
        }
        circle(400,n,p,150);

        n++;
    }

    close_lcd(p);

    return 0;

}